#ifndef BOARD_H
#define BOARD_H

#include <stdint.h>
#include <stdbool.h>

// 包含段码定义头文件
#include "segment_codes.h"

// LED引脚定义
#define LED1_PIN 25
#define LED2_PIN 0
#define LED3_PIN 0

// HT1621B引脚定义
#define HT1621B_CS_PIN 5
#define HT1621B_RD_PIN 9
#define HT1621B_WR_PIN 1
#define HT1621B_DATA_PIN 3

// 蜂鸣器配置参数
#define BUZZER_DEFAULT_GPIO 13

// 呼吸灯配置参数
#define BREATHING_PWM_GPIO 8

// GPIO引脚定义
#define DS3231_SDA_PIN 20
#define DS3231_SCL_PIN 21
#define DS3231_RST_PIN 17
#define DS3231_INT_PIN 18
// I2C实例
#define DS3231_I2C i2c0

#define KEY1_PIN 28 //倒计时
#define KEY2_PIN 2 //闹钟2
#define KEY3_PIN 26 //HOME
#define KEY4_PIN 14 //闹钟1

#define KEY_A_PIN 12//选择编码A
#define KEY_B_PIN 4//选择编码B
#define KEY_T_PIN 10//旋转编码确认

typedef enum
{
    SYSTEM_LED_TYPE_NONE = 0,
    SYSTEM_LED_TYPE_RED = 1,
    SYSTEM_LED_TYPE_GREEN = 2,
    SYSTEM_LED_TYPE_BLUE = 4,
} SYSTEM_LED_TYPE;

typedef enum
{
    SYSTEM_NORMAL = 0,
    SYSTEM_LEADOFF,
    SYSTEM_REPEAT,
    SYSTEM_POWER_LOW,
    SYSTEM_LOWPOWER,
    SYSTEM_CHARGING,
    SYSTEM_CHARGEOVER,
    SYSTEM_OAD,
    SYSTEM_DEVICE_INIT_ERROR,
    SYSTEM_TERMINATE = 0xFF,
} LED_ID;

typedef enum
{
    SYSTEM_RTC_MODE = 0,
    SYSTEM_ALARM1_MODE,
    SYSTEM_ALARM2_MODE,
    SYSTEM_COUNTDOWN_MODE,
    SYSTEM_CHARGING_MODE,
    SYSTEM_TERMINATE_MODE = 0xFF,
} SYSTEM_WORK_MODE;

// RTC显示页面类型枚举
typedef enum
{
    RTC_PAGE_TIME = 0, // 显示时间（时:分:秒）
    RTC_PAGE_DATE,     // 显示日期（月-日）和星期
    RTC_PAGE_YEAR,     // 显示年份（20XX）
    RTC_PAGE_COUNT     // 页面总数
} RTC_PAGE_TYPE;

typedef enum
{
    SYSTEM_WAKEUP_EVT_NONE = 0x00,
    SYSTEM_WAKEUP_EVT_KEY= 0x01,
    SYSTEM_WAKEUP_EVT_LONG_KEY = 0x02,
    SYSTEM_WAKEUP_EVT_ROTARY = 0x04,
    SYSTEM_WAKEUP_EVT_CLOCK = 0x08,
    SYSTEM_WAKEUP_EVT_CHARGE = 0x10,
} SYSTEM_WAKEUP_EVT;

// 倒计时状态枚举
typedef enum
{
    TICK_STATUS_STOPPED = 0,
    TICK_STATUS_SETTING,
    TICK_STATUS_RUNNING,
    TICK_STATUS_PAUSED,
    TICK_STATUS_FINISHED,
} tick_status_t;

typedef struct
{
    uint8_t powerpercent;
    uint32_t timetick;
    uint8_t componentstat[6];
    uint8_t breath_lead_status;
    uint8_t work_status;
    //uint8_t flash_status;
    uint8_t system_status;
} SYSTEM_STATE;

typedef struct
{
    int8_t seconds;
    int8_t minutes;
    int8_t hours;
    uint8_t weekday;
    uint8_t date;
    uint8_t month;
    uint8_t year;
} rtc_time_t;

typedef struct
{
    int8_t milliseconds;
    int8_t seconds;
    int8_t minutes;
} countdown_time_t;

typedef struct
{
    int8_t seconds;
    int8_t minutes;
    int8_t hours;
} alarm_time_t;

typedef struct
{
    union
    {
        countdown_time_t countdown_time;
        alarm_time_t alarm_time;
    };
    tick_status_t status;
} tick_time_t;

// 按键状态枚举
typedef enum
{
    KEY_RELEASED = 0,
    KEY_PRESSED,
    KEY_LONG_PRESSED
} key_state_t;

// 闹钟结构体
typedef struct
{
    tick_time_t time;
    bool repeat;         // 是否重复响铃
    uint8_t repeat_days; // 重复天数位图 (bit0=周日, bit1=周一, ..., bit6=周六)
    uint8_t id;          // 闹钟ID (1 or 2)
} alarm_t;

// 按键配置
#define COUNT_DOWN_NUM 10

#define MAIN_TASK_DELAY_MS 10

extern rtc_time_t current_time;
extern HourHigh_STRUCT HourHighTable[];
extern LCD_Display_8_STRUCT LCD_Display_8Table[];
extern LCD_Display_8_STRUCT MinuteHighTable[];
extern LCD_Display_8_STRUCT MinuteLowTable[];

extern SYSTEM_WAKEUP_EVT systemWakeupEvent;
extern SYSTEM_WORK_MODE systemWorkMode;
extern SYSTEM_STATE systemstate;

extern bool RTC_1S_INT;
extern tick_time_t down_time;
extern alarm_t alarms[2]; 
#endif
